package cn.zy.org.service.impl;

import cn.zy.basic.exception.BusinessException;
import cn.zy.basic.util.BaiduAiUtils;
import cn.zy.org.domain.Employee;
import cn.zy.org.domain.Shop;
import cn.zy.org.domain.ShopAuditLog;
import cn.zy.org.mapper.EmployeeMapper;
import cn.zy.org.mapper.ShopAuditLogMapper;
import cn.zy.org.mapper.ShopMapper;
import cn.zy.org.service.IShopService;
import cn.zy.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //一.校验
        //1.空值校验
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())){
            throw new BusinessException("信息不能为空");
        }
        //2.两次密码不一致校验
        if(!shop.getAdmin().getComfirmPassword().equals(shop.getAdmin().getPassword())){
            throw new BusinessException("两次密码不致");
        }
        //3.当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if(dbShop != null){//入驻过
            throw new BusinessException("当前店铺已经入驻过，直接登录,忘记密码请联系管理员!!!");
        }
        //二.百度AI智能审核【店铺名，logo是否合法】
        if(!BaiduAiUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称不合规!");
        }
        if(!BaiduAiUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new BusinessException("店铺Logo不合规");
        }

        //三：添加到数据库
        //1.先添加员工信息
        Employee admin = shop.getAdmin();
        employeeMapper.save(admin); //shop_id = null 会返回自增id3个属性
        //2.再添加店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);//会返回自增id
        //3.将店铺id更新到员工表中
        admin.setShop_id(shop.getId()); //id ,shopId
        employeeMapper.update(admin);
        //四：人工审核之后发送激活邮件
    }

    @Override
    public void auditPass(ShopAuditLog log) throws MessagingException {
        //1.改状态：1待审核 - 2审核通过待激活【1.先查再改 2.直接修改】
        shopMapper.updateState(2,log.getShopId());
        //2.添加审核日志
        log.setState(1);//审核通过
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);
        //3.发邮件给店长通知他去激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1160898405@qq.com"); //平台的邮箱
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h1>你的店铺已经注册</h1><img src='http://123.207.27.208/"+
                shop.getLogo()+"' ><a href='http://localhost:8080/shop/active/"+log.getShopId()+"'>点击该链接激活</a>",true);
        //收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(employee.getEmail()); //改邮件是店长的邮箱 - 真实有效
        javaMailSender.send(mimeMessage);
    }
    @Override
    public void auditReject(ShopAuditLog log) throws MessagingException {
        //1.改状态：1待审核 - 2审核通过待激活【1.先查再改 2.直接修改】
        shopMapper.updateState(4,log.getShopId());
        //2.添加审核日志
        log.setState(0);//审核通过
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);
        //3.发邮件给店长通知他重新修改信息入驻
      //  SimpleMailMessage message = new SimpleMailMessage();
      //  message.setFrom("meiyu.zy@qq.com"); //平台的邮箱
      //  message.setSubject("店铺审核失败通知");
      //  //true - 能够再内容中编写html标签 - 会解析
      //  Shop shop = shopMapper.loadById(log.getShopId());
      //  message.setText("<h1>店铺审核失败，请重新入驻</h1><a href='http://localhost:8081/#/register/'>点击该链接重新入驻</a>",true);

        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1160898405@qq.com"); //平台的邮箱
        helper.setSubject("店铺审核失败通知");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h1>店铺审核失败，请重新入驻</h1><img src='http://123.207.27.208/"+
                shop.getLogo()+"' ><a href='http://localhost:8081/#/register/'>点击该链接重新入驻</a>",true);
        //收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(employee.getEmail()); //改邮件是店长的邮箱 - 真实有效
        javaMailSender.send(mimeMessage);



      //  //收件人
      //  Employee employee = employeeMapper.loadById(shop.getAdminId());
      //  message.setTo(employee.getEmail()); //改邮件是店长的邮箱 - 真实有效




    }
    /*
    步骤：
        1.使用代码生成器生成t_shop_audit_log的后端基础代码
        2.实体类时间修改：private Date auditTime = new Date();
        3.前端Shop.vue中将shop_id改成ShopId - 后端才能获取到数据
        4.编写接口
        5.编写实现类
        6.Mapper方法
     */

}
